package com.theaf.basemodule.web.controller;

import com.theaf.basemodule.common.api.MenuService;
import com.theaf.basemodule.common.condition.MenuCondition;
import com.theaf.basemodule.common.dto.menu.MenuDTO;
import com.theaf.basemodule.common.vo.MenuVO;
import com.theaf.core.web.ResultHandler;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping(value="/menu")
@Api(value = "/menu",description = "菜单请求")
public class MenuController {
    private Logger logger = LoggerFactory.getLogger(UserController.class);

    @Autowired
    private MenuService menuService;

    @RequestMapping(value = "/list",  method = RequestMethod.POST)
    @ApiOperation(value = "菜单查询", notes = "菜单查询", response = MenuDTO.class)
    public Object list(@RequestBody @ApiParam(name="查询条件",value="传入json格式",required=true) MenuCondition condition){
        return ResultHandler.success(menuService.findList(condition));
    }

    @RequestMapping(value = "/listOrder/{id}",  method = RequestMethod.PUT)
    @ApiOperation(value = "菜单查询", notes = "菜单查询", response = MenuDTO.class)
    @ApiImplicitParam(value = "标识" , name = "id" , dataType = "long" , paramType = "path",required=true)
    public Object listOrder(@PathVariable("id") Long id){
        return ResultHandler.success(menuService.findSameLevelList(id));
    }

    @RequestMapping(value = "/get/{id}",  method = RequestMethod.GET)
    @ApiOperation(value = "获取菜单数据", notes = "获取菜单数据", response = MenuDTO.class)
    @ApiImplicitParam(value = "标识" , name = "id" , dataType = "long" , paramType = "path",required=true)
    public Object get(@PathVariable("id") Long id){
        return ResultHandler.success(menuService.findById(id));
    }


    @RequestMapping(value = "/save", method = RequestMethod.POST)
    @ApiOperation(value = "保存菜单", httpMethod = "POST" ,response = Long.class)
    public Object save(@RequestBody @Validated @ApiParam(name="菜单数据",value="传入json格式",required=true) MenuVO vo){
        return ResultHandler.success(menuService.save(vo));
    }

    @RequestMapping(value = "/delete/{id}",  method = RequestMethod.DELETE)
    @ApiOperation(value = "删除菜单", notes = "删除菜单", response = Boolean.class)
    @ApiImplicitParam(value = "菜单标识" , name = "id" , dataType = "long" , paramType = "path",required=true)
    public Object delete(@PathVariable("id") Long id){
        menuService.delete(id);
        return ResultHandler.success(true);
    }

    @RequestMapping(value = "/saveOrder", method = RequestMethod.POST)
    @ApiOperation(value = "保存菜单顺序", httpMethod = "POST" ,response = Long.class)
    public Object saveOrder(@RequestBody @Validated @ApiParam(name="菜单数据",value="传入json格式",required=true) List<Long> vo){
        menuService.saveSort(vo);
        return ResultHandler.success(true);
    }
}
